﻿using System;
using Model = ServiceGerenciador.Model;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    public string ActionToDo
    {
        get
        {
            if (this.Request.QueryString["do"] != null)
                return this.Request.QueryString["do"];
            return string.Empty;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUsuario.Focus();
        if (this.ActionToDo.Equals("logout"))
        {
            FormsAuthentication.SignOut();
            Response.Redirect("login.aspx");
        }
    }
    protected void btnEntrar_Click(object sender, EventArgs e)
    {
       if (string.IsNullOrEmpty(txtUsuario.Text) || string.IsNullOrEmpty(txtSenha.Text))
            return;

        var user = Model.User.DoLogin(txtUsuario.Text, txtSenha.Text);

        if (user != null)
        {
            this.Session["UltimoLogin"] = user.UltimoLogin;
            user.UltimoLogin = DateTime.Now;
            user.Save();
            FormsAuthentication.RedirectFromLoginPage(Convert.ToString(user.Id), true);
        }
        else
        {
            litError.Text = "Login inválido. Tente novamente";
        }
    }
}
